//
//  JsView.m
//  JsUtilitySdk
//
//  Created by Gikki Ares on 2023/5/23.
//

#import "JsView.h"
#import "JsLogManager.h"


const float MATCH_PARENT = -2;
const float WRAP_CONTENT = -1;
const float UNDEFINED = 0;
@implementation JsViewVm

- (instancetype)init {
	if(self = [super init]) {
		
	}
	return self;
}


@end

@interface JsView() {
	int mi_vmUpdateCount;
	UITapGestureRecognizer * mTap;
	id mTapTarget;
	SEL mTapSel;
	CAGradientLayer * mGradientLayer;
}
@end

@implementation JsView
@synthesize vm = mVm;

#pragma mark 1 LifeCircle
- (instancetype)init {
	if(self = [super init]) {
		mVm = [JsViewVm new];
	}
	return self;
}
- (instancetype)initWithVm:(JsViewVm *)vm {
	if(self = [super init]) {
		mVm = vm;
		//下一个runloop绘制
		[self updateVm];
	}
	return self;
}

//vc.view 从xib初始化,diao
- (instancetype)initWithCoder:(NSCoder *)coder {
	if(self = [super initWithCoder:coder]) {
		mVm = [JsViewVm new];
		mVm.width = self.frame.size.width;
		mVm.height = self.frame.size.height;
	}
	return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
	if(self = [super initWithFrame:frame]) {
		mVm = [self vm];
		mVm.width = frame.size.width;
		mVm.height = frame.size.height;
	}
	return self;
}


- (void)dealloc {
//	NSString * className = NSStringFromClass(self.class);
		//	jslogv_c(className,nil, @"dealloc");
}

#pragma mark 2 Interface
-(void)setFrame:(CGRect)frame {
	[super setFrame:frame];
}

- (void)setBounds:(CGRect)bounds {
	[super setBounds:bounds];
}


- (void)setVm:(JsViewVm *)vm {
	mVm = vm;
	[self updateVm];
}

-(void)updateVm {
	mi_vmUpdateCount ++;
	if(mi_vmUpdateCount == 1) {
		dispatch_async(dispatch_get_main_queue(), ^{
			[self renderVm];
			self->mi_vmUpdateCount = 0;
		});
	}
}

//尝试不在layoutSubviews中更改自己的大小.
- (void)layoutSubviews {
	[super layoutSubviews];
	if(mGradientLayer) {
		mGradientLayer.frame = self.bounds;
	}
}

-(void)renderVm {
	if(self.vm.backgroundColor) {
		self.backgroundColor = self.vm.backgroundColor;
		if(mGradientLayer) {
			[mGradientLayer removeFromSuperlayer];
			mGradientLayer = nil;
		}
	}
	else if(self.vm.backgroundColors) {
		mGradientLayer = [CAGradientLayer layer];
		NSMutableArray  * marr = [NSMutableArray array];
		[self.vm.backgroundColors enumerateObjectsUsingBlock:^(UIColor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
			CGColorRef cgColor = [obj CGColor];
			[marr addObject:(__bridge id)cgColor];
		}];
		mGradientLayer.colors = marr;
		mGradientLayer.startPoint = self.vm.startPoint;
		mGradientLayer.endPoint = self.vm.endPoint;
		if(self.vm.locations) {
			mGradientLayer.locations = self.vm.locations;
		}
		[self.layer insertSublayer:mGradientLayer atIndex:0];
	}
	
	
	
	if(self.vm.borderColor) {
		self.layer.borderColor = [self.vm.borderColor CGColor];
	}
	self.layer.masksToBounds = self.vm.cornderRadius > 0;
	self.layer.borderWidth = self.vm.borderWidth;
	self.layer.cornerRadius = self.vm.cornderRadius;
	
	if(mVm.shadowColor) {
		self.layer.shadowColor = [UIColor blackColor].CGColor;
		self.layer.shadowRadius = 5;
		self.layer.shadowOffset = CGSizeMake(3, 3);
		self.layer.shadowOpacity = 0.8;
	}
	else {
		self.layer.shadowColor = nil;
	}
	//设置显示声明的大小.
	CGRect _frame = self.frame;
	float width = self.frame.size.width;
	float height = self.frame.size.height;
	if(self.vm.dWidth > 0) {
		width = self.vm.dWidth;
	}
	if(self.vm.dHeight > 0) {
		height = self.vm.dHeight;
	}
	_frame.size.width = width;
	_frame.size.height = height;
	self.frame = _frame;
}



- (void)setIsClickable:(Boolean)isClickable {
	_isClickable = isClickable;
	if(isClickable) {
		if(!mTap) {
			mTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
			[self addGestureRecognizer:mTap];
		}
	}
	else {
		[self removeGestureRecognizer:mTap];
		mTap = nil;
	}
}

- (void)addTapTarget:(id)target selector:(SEL)selector {
	if(target) {
		[self setIsClickable:YES];
		mTapTarget = target;
		mTapSel = selector;
	}
}

- (void)onTap:(UIGestureRecognizer *)tag {
	if(mTap && mTapSel) {
		[mTapTarget performSelector:mTapSel];
	}
}

@end
